﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ershou.DAL;

namespace Ershou.BLL
{
    public class sp
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        public List<shangpin> GetProductByProductIdOrCategoryId(string productId)
        {
            if (!string.IsNullOrEmpty(productId))
            {
                return (from p in db.shangpin
                        where (p.name.Contains(productId) || p.type.Contains(productId)) && (p.size == "已审核" || p.size == "拍卖中")
                        orderby p.riqi descending
                        select p).ToList();
            }
            else
            {
                return (from p in db.shangpin
                        where p.size == "已审核" || p.size == "拍卖中"
                        orderby p.riqi descending
                        select p).ToList();
            }
        }
        public string select1(int ID)
        {
            shangpin x = (from p in db.shangpin
                          where p.id == ID
                          select p).FirstOrDefault();
            if (x != null)
            {
                return x.image;
            }
            else
            {
                return "~/Image/sp/暂无图片.gif";
            }
        }

        public string select2(int ID)
        {
            shangpin x = (from p in db.shangpin
                          where p.id == ID
                          select p).FirstOrDefault();
            if (x != null)
            {
                return x.price.ToString();
            }
            else
            {
                return x.qipai.ToString();
            }
        }
        public string select3(int ID)
        {
            shangpin x = (from p in db.shangpin
                          where p.id == ID
                          select p).FirstOrDefault();
            if (x != null)
            {
                return x.protect.ToString() + "元";
            }
            else
            {
                return "暂无显示";
            }
        }
        public string select4(int ID)
        {
            shangpin x = (from p in db.shangpin
                          where p.id == ID
                          select p).FirstOrDefault();
            if (x != null)
            {
                return x.qipai.ToString();
            }
            else
            {
                return "暂无显示";
            }
        }
        public string select5(int ID)
        {
            shangpin x = (from p in db.shangpin
                          where p.id == ID
                          select p).FirstOrDefault();
            if (x != null)
            {
                return x.zengchang.ToString();
            }
            else
            {
                return "暂无显示";
            }
        }
        public string select6(int ID)
        {
            shangpin x = (from p in db.shangpin
                          where p.id == ID
                          select p).FirstOrDefault();
            if (x != null)
            {
                return x.time.ToString()+"小时";
            }
            else
            {
                return "暂无显示";
            }
        }
        public string select7(int ID)
        {
            shangpin x = (from p in db.shangpin
                          where p.id == ID
                          select p).FirstOrDefault();
            if (x != null)
            {
                return x.name;
            }
            else
            {
                return "暂无显示";
            }
        }
        public DateTime select8(int ID)
        {
            shangpin x = (from p in db.shangpin
                          where p.id == ID
                          select p).FirstOrDefault();
            return Convert.ToDateTime(x.riqi);
        }
        public bool selectpm(int ID,int id)
        {
            paimaibiao x = (from p in db.paimaibiao
                          where p.yh_id == ID && p.sp_id==id
                          select p).FirstOrDefault();
            if (x != null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public string selectmj(int ID)
        {
            shangpin x = (from p in db.shangpin
                          where p.id == ID
                          select p).FirstOrDefault();
            if (x.maijia > 0)
            {
                return "出价人ID:"+Convert.ToString(x.maijia);
            }
            else
            {
                return "暂无出价";
            }
        }
        public void Update1(int id, int pri, int yh_id)
        {
            shangpin customer = (from c in db.shangpin
                               where c.id == id
                               select c).First();
            customer.price = pri;
            customer.maijia = yh_id;
            db.SubmitChanges();
        }
        public void Insert(int p, int e,int price)
        {
            paimaibiao customer = new paimaibiao
            {
                yh_id = p,
                sp_id = e,
                price = price
            };
            db.paimaibiao.InsertOnSubmit(customer);
            db.SubmitChanges();
        }

        public string select9(int ID)
        {
            shangpin x = (from p in db.shangpin
                          where p.id == ID
                          select p).FirstOrDefault();
            return x.Shows;
        }

        public bool select10(string name)
        {
            int x = (from p in db.shangpin
                          where p.name == name
                          select p).Count();
            if (x > 0)
            {
                return true;
            }
            return false;
        }

        public bool select11(int id,int n)
        {
            int x = (from p in db.shangpin
                     where p.id == id && p.master==n
                     select p).Count();
            if (x > 0)
            {
                return true;
            }
            return false;
        }

        public void Insert(string n, int m, string t, int p, string s, string i,
            int z, DateTime d, int pro)
        {
            shangpin customer = new shangpin
            {
                name = n,
                master = m,
                type = t,
                price = p,
                Shows = s,
                image = i,
                zengchang = z,
                riqi = d,
                time = 24,
                protect = pro,
                qipai = p,
                size = "未审核"
            };
            db.shangpin.InsertOnSubmit(customer);
            db.SubmitChanges();
        }

        public List<shangpin> GetAllOrder()
        {
            return (from p in db.shangpin
                    orderby p.riqi descending
                    select p).ToList();
        }

        public void UpdateOrder(int Id)
        {
            shangpin order = (from o in db.shangpin
                              where o.id == Id && o.size == "未审核"
                              select o).First();
            order.size = "已审核";
            db.SubmitChanges();
        }

        public List<shangpin> GetShangpins2(int i)
        {
            return (from p in db.shangpin
                    where p.master == i
                    orderby p.riqi descending
                    select p).ToList();
        }

        public void UpdateOrder2(int Id)
        {
            shangpin order = (from o in db.shangpin
                              where o.id == Id && o.size != "已结算"
                              select o).First();
            order.size = "已删除";
            db.SubmitChanges();
        }
        public string select12(int ID)
        {
            shangpin x = (from p in db.shangpin
                          where p.id == ID
                          select p).FirstOrDefault();
            return x.riqi.ToString().Trim();
        }
        public bool select13(string name)
        {
            int x = (from p in db.shangpin
                     where p.name == name
                     select p).Count();
            if (x > 1)
            {
                return true;
            }
            return false;
        }
        public void UpdateOrder3(int Id, string n, int m, string t, int p, string s, string i,
            int z, DateTime d, int pro)
        {
            shangpin order = (from o in db.shangpin
                              where o.id == Id && o.size != "拍卖中"
                              select o).First();
            order.name = n;
            order.master = m;
            order.type = t;
            order.price = p;
            order.Shows = s;
            order.image = i;
            order.zengchang = z;
            order.riqi = d;
            order.time = 24;
            order.protect = pro;
            order.qipai = p;
            order.size = "未审核";
            db.SubmitChanges();
        }
        public string select14(int ID)
        {
            shangpin x = (from p in db.shangpin
                          where p.id == ID
                          select p).FirstOrDefault();
            return x.type.ToString().Trim();
        }
        public string select15(int ID)
        {
            shangpin x = (from p in db.shangpin
                          where p.id == ID
                          select p).FirstOrDefault();
            return x.protect.ToString().Trim();
        }
        public string select16(int ID)
        {
            shangpin x = (from p in db.shangpin
                          where p.id == ID
                          select p).FirstOrDefault();
            return x.size.ToString().Trim();
        }
        public string select17(int ID)
        {
            shangpin x = (from p in db.shangpin
                          where p.id == ID
                          select p).FirstOrDefault();
            return x.image.ToString();
        }
        public void UpdateOrder4(int Id, string n, int m, string t, int p, string s,
            int z, DateTime d, int pro)
        {
            shangpin order = (from o in db.shangpin
                              where o.id == Id && o.size != "拍卖中"
                              select o).First();
            order.name = n;
            order.master = m;
            order.type = t;
            order.price = p;
            order.Shows = s;
            order.zengchang = z;
            order.riqi = d;
            order.time = 24;
            order.protect = pro;
            order.qipai = p;
            order.size = "未审核";
            db.SubmitChanges();
        }
        public void UpdateOrder5(int Id,int i)
        {
            DateTime now = DateTime.Now;
            DateTime tomorrow = now.AddDays(1);
            try
            {
                shangpin order = (from o in db.shangpin
                                  where o.id == Id && o.riqi < DateTime.Now.AddDays(-1) && o.maijia==i
                                  select o).First();
                order.size = "已结算";
                db.SubmitChanges();
            }
            catch
            {
                ;
            }
        }
        public string select18(int ID)
        {
            shangpin x = (from p in db.shangpin
                          where p.id == ID
                          select p).FirstOrDefault();
            if (x.Shows == null)
            {
                return "";
            }
            else
            {
                return x.Shows.ToString();
            }
        }
    }
}
